<?php

	namespace login\view;

		require_once ("./common/Filter.php");
		require_once ("./login/model/LoginObserver.php");

		class LoginView implements \login\model\LoginObserver
		{

			private static $LOGOUT = "logout";
			private static $LOGIN = "login";
			private static $USERNAME = "LoginView::UserName";
			private static $NEWUSERNAME = "LoginView::NewUserName";
			private static $PASSWORD = "LoginView::Password";
			private static $NEWPASSWORD = "LoginView::NewPassword";
			private static $RepeatNEWPASSWORD = "LoginView::RepeatNewPassword";
			private static $CHECKED = "LoginView::Checked";
			private static $CREATEACCOUNT = "create_account";

			private $message = "";
			private $myMessage = "";
			private $myUserNameMemory = "";

			public function getLoginBox($myMessage)
			{

				$user = $this->getUserName();
				$checked = $this->userWantsToBeRemembered() ? "checked=checked" : "";
				$this->myMessage = $myMessage;
				$this->myMessage = implode("<br>", $this->myMessage);
				$html = "
			<form action='?" . self::$LOGIN . "' method='post' enctype='multipart/form-data'>
				<fieldset>
					$this->message
					<a  href=\"?newAccount\" id=\"somethingsomething\">Skapa ny användare</a><br><br>
					$this->myMessage <br><br>
					<legend>Login - Skriv in användarnamn och lösenord</legend>
					<label for='UserNameID' >Användarnamn :</label>
					<input type='text' size='20' name='" . self::$USERNAME . "' id='UserNameID' value='$user' />
					<label for='PasswordID' >Lösenord  :</label>
					<input type='password' size='20' name='" . self::$PASSWORD . "' id='PasswordID' value='' />
					<label for='AutologinID' >Håll mig inloggad  :</label>
					<input type='checkbox' name='" . self::$CHECKED . "' id='AutologinID' $checked/>
					<input type='submit' name=''  value='Logga in' />
				</fieldset>
			</form>";

				return $html;

			}


			public function getCreateAccoutBox($myMessage, $myTempName)
			{


				
				$this->myMessage = $myMessage;
				$this->myMessage = implode("<br>", $this->myMessage);
				$html = "
			<form action='?" . self::$CREATEACCOUNT . "' method='post' enctype='multipart/form-data'>
				<fieldset>
					 $this->message
					<a  href=\"?login\" id=\"loginPageButton\">Tillbaka till början</a><br><br>
					$this->myMessage <br><br>
					<legend>Registrera ny användare - Skriv in användarnamn och lösenord</legend>
					<label for='UserNameID' >Nytt Namn :</label>
					<input type='text' size='20' name='" . self::$NEWUSERNAME . "' id='NewUserNameID' value='$myTempName' /><br><br>
					<label for='PasswordID' >Lösenord  :</label>
					<input type='password' size='20' name='" . self::$NEWPASSWORD . "' id='NewPasswordID' value='' /><br><br>
					<label for='PasswordID' >Repetera Lösenord  :</label>
					<input type='password' size='20' name='" . self::$RepeatNEWPASSWORD . "' id='RepeatNewPasswordID' value='' /><br><br>
					<input type='submit' name=''  value='Skapa' />
				</fieldset>
			</form>";

				return $html;

			}


			public function getUserCredentials()
			{
				if ($this->hasCookies())
				{
					return \login\model\UserCredentials::createWithTempPassword(new \login\model\UserName($this->getUserName()), $this->getTemporaryPassword());
				}
				else
				{

					//kallar på createFromClientData i UserCredentials i model
					//skickar med UserName och Password
					return \login\model\UserCredentials::createFromClientData(new \login\model\UserName($this->getUserName()), \login\model\Password::fromCleartext($this->getPassword()));
				}
			}


			public function isLoggingOut()
			{
				return isset($_GET[self::$LOGOUT]);
			}


			public function isLoggingIn()
			{
				if (isset($_GET[self::$LOGIN]))
				{
					return true;
				}
				else if ($this->hasCookies())
				{
					return true;
				}
				else
				{
					return false;
				}
			}


			public function getLogoutButton()
			{
				return "$this->message <a href='?" . self::$LOGOUT . "'>Logga ut</a>";
			}


			public function loginFailed()
			{
				if ($this->hasCookies())
				{
					$this->message = "<p>Felaktig information i cookie</p>";
					$this->removeCookies();
				}
				else
				{

					if ($this->getUserName() == "")
					{
						$this->message .= "<p>Användarnamn saknas</p>";
					}
					else if ($this->getPassword() == "")
					{
						$this->message .= "<p>Lösenord saknas</p>";
					}
					else
					{
						$this->message = "<p>Felaktigt användarnamn och/eller lösenord</p>";
					}
				}
			}


			public function loginOK(\login\model\TemporaryPasswordServer $tempCookie)
			{

				if ($this->userWantsToBeRemembered() || $this->hasCookies())
				{
					if ($this->hasCookies())
					{
						$this->message = "<p>Inloggning lyckades via cookies</p>";
					}
					else
					{
						$this->message = "<p>Inloggning lyckades och vi kommer ihåg dig nästa gång</p>";
					}

					$expire = $tempCookie->getExpireDate();
					setcookie(self::$USERNAME, $this->getUserName(), $expire);
					setcookie(self::$PASSWORD, $tempCookie->getTemporaryPassword(), $expire);
				}
				else
				{
					$this->message = "<p>Inloggning lyckades</p>";
				}

			}


			public function doLogout()
			{
				$this->removeCookies();

				$this->message = "<p>Du har nu loggat ut</p>";
			}


			private function getUserName()
			{
				if (isset($_POST[self::$USERNAME]))
					return \Common\Filter::sanitizeString($_POST[self::$USERNAME]);
				else if (isset($_COOKIE[self::$USERNAME]))
					return \Common\Filter::sanitizeString($_COOKIE[self::$USERNAME]);
				else
					return "";
			}


			private function getPassword()
			{
				if (isset($_POST[self::$PASSWORD]))
					return \Common\Filter::sanitizeString($_POST[self::$PASSWORD]);
				else
					return "";
			}


			private function userWantsToBeRemembered()
			{
				return isset($_POST[self::$CHECKED]);
			}


			public function getTemporaryPassword()
			{
				if (isset($_COOKIE[self::$PASSWORD]))
				{
					$fromCookieString = \Common\Filter::sanitizeString($_COOKIE[self::$PASSWORD]);
					return \login\model\TemporaryPasswordClient::fromString($fromCookieString);
				}
				else
				{
					return \login\model\TemporaryPasswordClient::emptyPassword();
				}
			}


			private function hasCookies()
			{
				return isset($_COOKIE[self::$PASSWORD]) && isset($_COOKIE[self::$USERNAME]);
			}


			private function removeCookies()
			{

				unset($_COOKIE[self::$USERNAME]);
				unset($_COOKIE[self::$PASSWORD]);

				$expireNow = time() - 1;
				setcookie(self::$USERNAME, "", $expireNow);
				setcookie(self::$PASSWORD, "", $expireNow);
			}


		}
